<?php

namespace App\Http\Controllers\Api;

use App\Models\Chat;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

// 聊天信息控制器
class ChatsController extends Controller
{

    // 添加聊天信息
    public function store(Request $request)
    {
        $user = $request->user(); // 这里使用了获取用户信息中间件
//        if ($user->provider !== UserPolicy::PROVIDER_WECHAT) {
//            // 抛出异常
//        }

        $chat = [
            'sequence' => intval($request->input('sequence')),
            'type' => $request->input('sequence'),
            'send_time' => Carbon::now()->toDateTimeString(),
            'content' => $request->input('content'),
            'user_id' => $user->id,
            'live_id' => $request->input('live_id')
        ];

        // 把聊天信息保存到list数据类型中
        Redis::rPush('chat', json_encode($chat));

        return $this->success('add success');
    }
}
